﻿using Prism.Commands;
using Prism.Services.Dialogs;
using SmartPlant.Core;
using SmartPlant.Core.Mvvm;
using SmartPlant.Model.Biz;
using SmartPlant.Modules.Base.UIEntity;
using SmartPlant.WinCore;
using System.Collections.ObjectModel;
using System.Windows.Input;
using Telerik.Windows.Data;
using DialogParameters = Prism.Services.Dialogs.DialogParameters;

namespace SmartPlant.Modules.Base.ViewModels
{
    public class IndexTableColumnSelectViewModel : DialogViewModelBase, IDialogAware
    {
        private DelegateCommand<object> _searchCommand;
        private string _searchText;

        public IndexTableColumnSelectViewModel(IDialogService dialogService) : base(dialogService)
        {
            this.Title = "列定义";
        }
        private List<DynamicPropertyInfo> _columns;
        private List<DynamicPropertyInfo> _items;
        public List<DynamicPropertyInfo> Items
        {
            get
            {
                return _items;
            }
            set
            {
                SetProperty(ref _items, value);
            }
        }
        private ObservableCollection<DynamicPropertyInfo> _selectedItems;
        public ObservableCollection<DynamicPropertyInfo> SelectedItems
        {
            get
            {
                return this._selectedItems;
            }
            set
            {
                SetProperty(ref _selectedItems, value);
            }
        }
        public string SearchText
        {
            get { return _searchText; }
            set { SetProperty(ref _searchText, value); }
        }
        /// <summary>
        /// 搜索command
        /// </summary>
        public ICommand SearchCommand
        {
            get
            {
                if (_searchCommand != null) return _searchCommand;
                _searchCommand = new DelegateCommand<object>(onSearchCommand);
                return _searchCommand;
            }
        }
        /// <summary>
        /// 搜索
        /// </summary>
        /// <param name="obj"></param>
        private void onSearchCommand(object obj)
        {
            foreach (var item in Items)
            {
                if (SelectedItems.Any(x => x.DisplayName == item.DisplayName))
                {
                    item.IsVisible = true;
                }
                else
                {
                    item.IsVisible = false;
                }
            }
            if (string.IsNullOrEmpty(SearchText))
            {
                Items = _columns;
                SelectedItems = new ObservableCollection<DynamicPropertyInfo>(Items.Where(x => x.IsVisible).ToList());
            }
            else
            {
                Items = _columns.Where(x => x.DisplayName.ToUpper().Contains(SearchText.ToUpper())).ToList();
                SelectedItems = new ObservableCollection<DynamicPropertyInfo>(Items.Where(x => x.IsVisible).ToList());
            }
        }

        protected override DialogResult RaiseDialogOk()
        {
            foreach (var item in Items)
            {
                if (SelectedItems.Any(x => x.DisplayName == item.DisplayName))
                {
                    item.IsVisible = true;
                }
                else
                {
                    item.IsVisible = false;
                }
            }
            foreach (var key in _columns.Where(x => x.PrimaryKey == true))
            {
                key.IsVisible = true;
            }

            var res = _columns.Where(x => x.IsVisible).ToList();//这个_columns 到底是哪里被修改的。。。明明没有被修改，但是就是能识别到最新的IsVisible的集合
            var par = new DialogParameters
            {
                { "Items", res }
            };
            return new DialogResult(ButtonResult.OK, par);
        }


        public override void OnDialogOpened(IDialogParameters parameters)
        {
            //SPIColName 和 DisplayName
            Items = _columns = parameters.GetValue<List<DynamicPropertyInfo>>("Items");
            SelectedItems = new ObservableCollection<DynamicPropertyInfo>(Items.Where(x => x.IsVisible).ToList());
        }
    }
}
